#include "Exp1.h"

void Exp1()
  {
    cleardevice();
    int Still = 1;
    Object Cursor, *pCursor = &Cursor;
    Food FoodArr[7], *pFood = FoodArr;
    InitFood(pFood);

    InitExp1Cursor(pCursor);
    char Map[350], *pMap = Map;
    ReadMapN(pMap, 1);

    OptIntro(1);

    while (Still)
      {
        OptMap1();
        OptFood(pFood);
        OptCursor(pCursor);
        if (pCursor->HP == 10)
          OptDoorLight(1152, 11);
        PlayerAct(pCursor, pMap, &Still);
        CouldEat(pCursor, pFood);
        Exp1Solved(pCursor, &Still);
        FlushBatchDraw();
      }
    OptMenuBk();
  }
void InitExp1Cursor(Object *pCursor)
  {
    pCursor->HP = 3;
    pCursor->X = 15, pCursor->Y = 10;
    pCursor->GridX = 0, pCursor->GridY = 0;
  }
void InitFood(Food *pFood)
  {
    pFood[0].GridX = 2, pFood[0].GridY = 3;
    pFood[1].GridX = 8, pFood[1].GridY = 3;
    pFood[2].GridX = 14, pFood[2].GridY = 3;
    pFood[3].GridX = 10, pFood[3].GridY = 7;
    pFood[4].GridX = 5, pFood[4].GridY = 12;
    pFood[5].GridX = 11, pFood[5].GridY = 12;
    pFood[6].GridX = 17, pFood[6].GridY = 12;

    for ( int i = 0; i < 7; i++)
      pFood[i].Exist = 1;
  }
void CouldEat(Object *pCursor, Food *pFood)
  {
    for ( int i = 0; i < 7; i++, pFood++)
      {
        if (pFood->Exist)
          {
            if (pFood->GridX == pCursor->GridX)
              if (pFood->GridY == pCursor->GridY)
                EatFood(pCursor, pFood);
          }
      }
  }
void EatFood(Object *pCursor, Food *pFood)
  {
    pFood->Exist = 0;
    pCursor->HP++;
  }

//Output
void OptMap1()
  {
    IMAGE Map1;
    loadimage(&Map1, _T("Pic/Exp1/Map1.png"));
    putimage(0, 0, &Map1);
  }
void OptFood(Food *pFood)
  {
    for ( int i = 0; i < 7; i++)
      {
        if (pFood->Exist)
          OptSingleFood(pFood);
        pFood++;
      }
  }
void OptSingleFood(Food *pFood)
  {
    int X = 0, Y = 0;
    GridXY2XY(pFood->GridX, pFood->GridY, &X, &Y);
    IMAGE ImgFood;
    loadimage(&ImgFood, _T("Pic/Exp1/Food.png"));
    TransparentImage(NULL, X, Y, &ImgFood);
  }

void Exp1Solved(Object *pCursor, int *Still)
  {
    int X = pCursor->X, Y = pCursor->Y;
    if (pCursor->HP == 10)
      {
        if ( (pCursor->GridX == 23)||
             (pCursor->GridX == 24)
           )
          if ( (pCursor->GridY == 1)||
               (pCursor->GridY == 2)
             )
              {
                Accomplish('1');
                *Still = 0;
              }
      }
  }
